﻿#include "actioncontrol.h"
#include "config.h"
#include "logindlg.h"
#include "mainwindow.h"
#include "db.h"
#include <QMessageBox>
#include <QInputDialog>
#include<qtextcodec.h>
#pragma execution_character_set("utf-8")

ActionControl::ActionControl(QObject *parent) :
    QAction(parent), domainObject(NULL), main(parent)
{

    QTextCodec* code=QTextCodec::codecForName("utf-8");

    QTextCodec::setCodecForLocale(code);
}

void ActionControl::setId(int id)
{
    this->id = id;
}

int ActionControl::getId()
{
    return id;
}

void ActionControl::setType(int type)
{
    this->type = type;
}

int ActionControl::getType()
{
    return type;
}

void ActionControl::doControl()
{
    bool ret = false;
    QList<GYSCADALogItem> logItems;

    if (domainObject->countChecked() > 0) {

        /* Authorize */
        LoginDlg dlgLogin(true);
        if (dlgLogin.exec() == QDialog::Accepted) {

            QList<QString> descriptions = domainObject->getCheckedDescription();

            if (domainObject->getControlInfos().contains(id)) {
                ControlInfo controlInfo = domainObject->getControlInfos()[id];

                if (type == ControlInfo::ControlTypeYK) {
                    ret = domainObject->doControl(id);
                } else {
                    bool ok;
                    double value = QInputDialog::getDouble(
                        NULL, tr("控制数值"),
                        GYSCADAConfig::getInstance()->getControlDlgTitle(id),
                        GYSCADAConfig::getInstance()->getControlDefaultValue(id),
                        GYSCADAConfig::getInstance()->getControlMinValue(id),
                        GYSCADAConfig::getInstance()->getControlMaxValue(id),
                        GYSCADAConfig::getInstance()->getControlDecimals(id), &ok);
                    if (ok) {
                        ret = domainObject->doControl(id, value);
                    } else {
                        return;
                    }
                }
            } else {
                ret = false;
            }

            if (ret) {
                QMessageBox::information(NULL, tr("OK"), tr("发送控制命令成功"));
                for (QList<QString>::iterator it = descriptions.begin();
                     it != descriptions.end();
                     ++it) {
                    GYSCADALogItem logItem;
                    logItem.userId = GYSCADAConfig::getInstance()->getUserId();
                    // operation
                    logItem.operation = tr("测试");
                    logItem.result = tr("成功");
                    logItem.equipment = *it;
                    logItem.timestamp = QDateTime::currentDateTime();
                    logItems.append(logItem);
                }
            } else {
                QMessageBox::warning(NULL, tr("Warning"), tr("发送控制命令失败"));
                for (QList<QString>::iterator it = descriptions.begin();
                     it != descriptions.end();
                     ++it) {
                    GYSCADALogItem logItem;
                    logItem.userId = GYSCADAConfig::getInstance()->getUserId();
                    // operation
                    logItem.operation = tr("测试");
                    logItem.result = tr("失败");
                    logItem.equipment = *it;
                    logItem.timestamp = QDateTime::currentDateTime();
                    logItems.append(logItem);
                }
            }

            if (!GYSCADADB::getInstance()->record(logItems)) {
                QMessageBox::critical(NULL, tr("错误"), tr("日志记录失败！"));
            }

            if (main != NULL) {
               // ((MainWindow*)main)->clearChecked();
            }
        }
    } else {
        QMessageBox::information(NULL, tr("Nothing"), tr("请选择要控制的风机"));
    }
}

void ActionControl::doReload(DomainObject *domainObject)
{
    this->domainObject = domainObject;
    if (this->domainObject == NULL) {
        setDisabled(true);
    } else {
        if (this->domainObject->getControlInfos().contains(id)) {
            setEnabled(true);
        } else {
            setDisabled(true);
        }
    }
}

void ActionControl::doControlOne()
{
    bool ret = false;
    QList<GYSCADALogItem> logItems;

    if (domainObject->countChecked() > 0) {

        /* Authorize */
        LoginDlg dlgLogin(true);
        if (dlgLogin.exec() == QDialog::Accepted) {

            QList<QString> descriptions = domainObject->getCheckedDescription();

            if (domainObject->getControlInfos().contains(id)) {
                ControlInfo controlInfo = domainObject->getControlInfos()[id];

                if (type == ControlInfo::ControlTypeYT) {
                    ret = domainObject->doControl(id);

                } else {
                    bool ok;
                    double value = QInputDialog::getDouble(
                        NULL, tr("控制数值"),
                        GYSCADAConfig::getInstance()->getControlDlgTitle(id),
                        GYSCADAConfig::getInstance()->getControlDefaultValue(id),
                        GYSCADAConfig::getInstance()->getControlMinValue(id),
                        GYSCADAConfig::getInstance()->getControlMaxValue(id),
                        GYSCADAConfig::getInstance()->getControlDecimals(id), &ok);
                    if (ok) {
                        ret = domainObject->doControl(id, value);
                    } else {
                        return;
                    }
                }
            } else {
                ret = false;
            }

            if (ret) {
                QMessageBox::information(NULL, tr("OK"), tr("发送控制命令成功"));
                for (QList<QString>::iterator it = descriptions.begin();
                     it != descriptions.end();
                     ++it) {
                    GYSCADALogItem logItem;
                    logItem.userId = GYSCADAConfig::getInstance()->getUserId();
                    // operation
                    logItem.operation = tr("测试");
                    logItem.result = tr("成功");
                    logItem.equipment = *it;
                    logItem.timestamp = QDateTime::currentDateTime();
                    logItems.append(logItem);
                }
            } else {
                QMessageBox::warning(NULL, tr("Warning"), tr("发送控制命令失败"));
                for (QList<QString>::iterator it = descriptions.begin();
                     it != descriptions.end();
                     ++it) {
                    GYSCADALogItem logItem;
                    logItem.userId = GYSCADAConfig::getInstance()->getUserId();
                    // operation
                    logItem.operation = tr("测试");
                    logItem.result = tr("失败");
                    logItem.equipment = *it;
                    logItem.timestamp = QDateTime::currentDateTime();
                    logItems.append(logItem);
                }
            }

            if (!GYSCADADB::getInstance()->record(logItems)) {
                QMessageBox::critical(NULL, tr("错误"), tr("日志记录失败！"));
            }

            if (main != NULL) {
               // ((MainWindow*)main)->clearChecked();
            }
        }
    } else {
        QMessageBox::information(NULL, tr("Nothing"), tr("请选择要控制的风机"));
    }
}
